package dca;

public class Symptom implements Comparable<Object> {
	
	private String name;
	private Severity severity;
	private String sortBy="Alphabet";
	
	
	/**
	 * Constructs a symptom with a given name and severity.
	 * @param name
	 * @param severity
	 */
	public Symptom(String name,Severity severity){
		this.name = name;
		this.severity = severity;
	}
	public Symptom(String name, String SeverityString) throws BadDataException{
		this.name = name;
		boolean severityMatched=false;
		for (Severity s:Severity.values())
		{
			if(s.toString().equalsIgnoreCase(SeverityString))
			{
				this.severity=s;
				severityMatched=true;
			}
		}
		if(!severityMatched){
			throw new BadDataException("Couldn't match described severity: "+SeverityString);
		}
	}
	
	public String getName(){
		return name;
	}
	
	public Severity getSeverity(){
		return this.severity;
	}
	
	public String toString(){
		return name+","+severity.toString();		
	}
	
	
	/**
	 * Used to change to sorting state of symptoms. Can be sorted by the alphabet or grouped by severity. Default sort by alphabet. Invalid input is ignored.
	 * @param sortBy
	 * @throws BadDataException 
	 */
	public void sortBy(String sortBy) throws BadDataException{
		if(sortBy.equalsIgnoreCase("Alphabet") || sortBy.equalsIgnoreCase("Severity"))
		this.sortBy = sortBy;
		else
			throw new BadDataException("Could not recognice sort command : "+sortBy+". Instead sorting by "+this.sortBy);
	
	}
	
	public int compareTo(Object anotherObject){
		if (!(anotherObject instanceof Symptom)){
			throw new ClassCastException("Can only compare symptons to other symptoms");
		}
		Symptom anotherSymptom = (Symptom) anotherObject;
		if(sortBy.equalsIgnoreCase("Severity"))
		{
			if(this.severity.getPriority()> anotherSymptom.severity.getPriority())
			{
				return -1; 
			}
			if(this.severity.getPriority()< anotherSymptom.severity.getPriority())
			{
				return 1; 
			}
			else{return 0;}
			
		}
		
		return this.name.compareToIgnoreCase(anotherSymptom.name);
		
		
	}
	
	//I override equals because i don't want the default "==" but to compare the names.
	@Override
	public boolean equals(Object anotherObject){
		if(!(anotherObject instanceof Symptom))
			throw new ClassCastException("Can only compare symptons to other symptoms");
		Symptom anotherSymptom = (Symptom) anotherObject;
		if(this.name.equalsIgnoreCase(anotherSymptom.name.trim()))
				return true;
		else{return false;}
			
		
	}
	

}
